<template>
  <form @submit.prevent="onsubmit">
    <va-input
      v-model="username"
      type="text"
      :label="'用户名'"
      :error="!!usernameErrors.length"
      :error-messages="usernameErrors"
    />

    <va-input
      v-model="password"
      type="password"
      :label="'密码'"
      :error="!!passwordErrors.length"
      :error-messages="passwordErrors"
    />

<!--    <div class="auth-layout__options d-flex justify&#45;&#45;space-between">-->
<!--      <va-input-->
<!--        v-model="checkcode"-->
<!--        type="text"-->
<!--        :label="'验证码'"-->
<!--        :error="!!checkcodeErrors.length"-->
<!--        :error-messages="checkcodeErrors"-->
<!--      />-->
<!--      <div class="ml-3" @click="getcode">-->
<!--        <img :src="checkcodeurl" />-->
<!--      </div>-->
<!--    </div>-->

    <div class="d-flex justify--center mt-3">
      <va-button type="submit" class="my-0">登陆</va-button>
    </div>
  </form>
</template>

<script>
export default {
  name: 'login',
  data () {
    return {
      username: '',
      password: '',
      // checkcode: '',
      // checkcodeurl: '/api/getcode',
      usernameErrors: [],
      passwordErrors: [],
      // checkcodeErrors: [],
    }
  },
  computed: {
    formReady () {
      return !this.usernameErrors.length && !this.passwordErrors.length
    },
  },
  methods: {
    //     getcode() {
    //   this.checkcodeurl = this.checkcodeurl + '?'
    // },
    onsubmit() {
      this.usernameErrors = this.username ? [] : ['Username is required']
      this.passwordErrors = this.password ? [] : ['Password is required']
      // this.checkcodeErrors = this.checkcode ? [] : ['Checkcode is required']
      if (!this.formReady) {
        return
      }
      var fd = new FormData();
      fd.append('username',this.username)
      fd.append('password',this.password)
      // fd.append('code',this.checkcode)
      var _this=this
      this.$http.post('http://8.135.35.123:9000/managerLogin',fd)
      .then(function(response) {
        console.log(response)
        switch (response.data.code) {
          // case 80500:
             // _this.checkcodeErrors=['验证码错误']
             // _this.showmsg('验证码错误')
 // _this.checkcode=''
 //            break;
          case 80302:
            _this.usernameErrors = ['用户不存在']
            _this.showmsg('用户不存在')
_this.username=_this.password
            break;
           case 80303:
            _this.passwordErrors = ['密码错误']
            _this.showmsg('密码错误')
_this.password
            break;
            case 80200:
            _this.showmsg('登陆成功')
            localStorage.setItem('username', _this.username);
            _this.$router.push({ name: 'welecome' })

            break;
          default:
            _this.showmsg('未知错误')
            break;
        }

        },
        function(err){
          _this.showmsg('网络错误')
        }
      )
    },
    showmsg(msg) {
            this.showToast(
        msg,
        {
          position: 'top-center',
          duration: 2500,
          fullWidth: false,
        },
      )

    }
  },
}
</script>

<style lang="scss">
</style>
